package tools;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import static org.apache.commons.lang3.StringUtils.isBlank;

public class PinYinUtils {
    /**
     * 获取小区名字拼音缩写
     * "区" 是多音字,但这个 就读 "qu".把其他的干掉
     *
     * @param chines
     * @return
     */
    public static String getFirstEstateEnName(String chines) {
        StringBuffer pinyinName = new StringBuffer("");
        String firstSpell = null;
        if (StringUtils.isNotBlank(chines)) {
            String firstSpell1 = getFirstSpell(chines);
            if ((!chines.endsWith("区")) && (!chines.endsWith("弄"))) {
                return firstSpell1;
            } else {
                firstSpell = firstSpell1;
            }
        }

        if (StringUtils.isNotBlank(firstSpell)) {
            String[] split = firstSpell.split(",");
            if (split != null && split.length > 0) {
                for (String s : split) {
                    if (StringUtils.isNotBlank(s)) {
                        //弄(n,l),小区(q,o)
                        if ((!s.endsWith("o")) && (!s.endsWith("l"))) {
                            pinyinName.append(s).append(",");
                        }
                    }
                }
            }
        }
        String s = pinyinName.toString();
        if (s.endsWith(",")) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

    /**
     * 获取汉字串拼音首字母
     *
     * @param chines
     * @return
     */
    public static String getFirstSpell(String chines) {
        if (isBlank(chines)) {
            return null;
        }
        StringBuffer pinyinName = new StringBuffer();
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : nameChar) {
            if (c > 128) {
                try {
                    //取得当前汉字的所有全拼
                    String[] strs = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat);
                    if (strs != null) {
                        for (int j = 0; j < strs.length; j++) {
                            //取得首字母
                            pinyinName.append(strs[j].charAt(0));
                            if (j != strs.length - 1) {
                                pinyinName.append(",");
                            }
                        }
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }

            } else {
                pinyinName.append(c);
            }
            pinyinName.append(" ");
        }
        return parseTheChineseByObject(discountTheChinese(pinyinName.toString()));
    }

    /**
     * 去除重复字重复数据
     *
     * @param theStr
     */
    private static List<Map<String, Integer>> discountTheChinese(String theStr) {
        // 去除重复拼音后的拼音列表
        List<Map<String, Integer>> mapList = new ArrayList<Map<String, Integer>>();
        // 用于处理每个字的多音字，去掉重复
        Map<String, Integer> onlyOne = null;
        String[] firsts = theStr.split(" ");
        // 读出每个汉字的拼音
        for (String str : firsts) {
            onlyOne = new Hashtable<String, Integer>();
            String[] china = str.split(",");
            // 多音字处理
            for (String s : china) {
                Integer count = onlyOne.get(s);
                if (count == null) {
                    onlyOne.put(s, 1);
                } else {
                    onlyOne.remove(s);
                    count++;
                    onlyOne.put(s, count);
                }
            }
            mapList.add(onlyOne);
        }
        return mapList;
    }

    /**
     * 解析并组合拼音，对象合并方案(推荐使用)
     *
     * @param list
     */
    private static String parseTheChineseByObject(List<Map<String, Integer>> list) {
        Map<String, Integer> first = null; // 用于统计每一次,集合组合数据
        // 遍历每一组集合
        for (Map<String, Integer> stringIntegerMap : list) {
            // 每一组集合与上一次组合的Map
            Map<String, Integer> temp = new Hashtable<String, Integer>();
            // 第一次循环，first为空
            if (first != null) {
                // 取出上次组合与此次集合的字符，并保存
                for (String s : first.keySet()) {
                    for (String s1 : stringIntegerMap.keySet()) {
                        String str = s + s1;
                        temp.put(str, 1);
                    }
                }
                // 清理上一次组合数据
                if (!temp.isEmpty()) {
                    first.clear();
                }
            } else {
                for (String s : stringIntegerMap.keySet()) {
                    temp.put(s, 1);
                }
            }
            // 保存组合数据以便下次循环使用
            if (!temp.isEmpty()) {
                first = temp;
            }
        }
        StringBuilder returnStr = new StringBuilder();
        if (first != null) {
            // 遍历取出组合字符串
            for (String str : first.keySet()) {
                returnStr.append(str).append(",");
            }
        }
        if (returnStr.length() > 0) {
            returnStr = new StringBuilder(returnStr.substring(0, returnStr.length() - 1));
        }
        if (returnStr.length() > 240) {
            return returnStr.substring(0, 240);
        }
        return returnStr.toString();
    }

    /**
     * 获取汉字串拼音，英文字符不变
     *
     * @param chinese 汉字串
     * @return 汉语拼音
     */
    public static String getFullSpell(String chinese) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : arr) {
            if (c > 128) {
                try {
                    String[] strings = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat);
                    if (strings == null || strings.length == 0) {
                        pybf.append(" ");
                    } else {
                        pybf.append(strings[0]);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(c);
            }
        }
        String s = pybf.toString();
        //多空格替换成一个空格
        s = s.replaceAll(" +", " ");
        return s;
    }

    public static String aa() {
        StringBuffer a = new StringBuffer();
        for (int i = 0; i < 100000; i++) {
            a.append(i);
            a.append(",");
        }

        return a.toString();
    }

    public static String bb(String b) {
        StringBuffer a = new StringBuffer();
        for (int i = 0; i < 100000; i++) {
            a.append(i);
            a.append(b);
        }
        System.out.println(a.toString().split(",").length);
        return a.toString();
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(() -> bb(",")).start();
        }


    }

}
